﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;

namespace Dragon
{
    public partial class wfDeviceTracking1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtFromDate.Text = DateTime.Now.ToString("MM") + "/" + "01/" + DateTime.Now.ToString("yyyy");
                this.txtToDate.Text = DateTime.Now.ToString("MM/dd/yyyy");                
            }
        }

        protected void ddlProvince_DataBound(object sender, EventArgs e)
        {
            this.ddlProvince.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlProvince.SelectedIndex = 0;
        }

        protected void ddlUser_DataBound(object sender, EventArgs e)
        {
            if (ddlProvince.SelectedIndex == 0)
                this.ddlUser.Items.Clear();
            this.ddlUser.Items.Insert(0, new ListItem("Tất cả", ""));
            this.ddlUser.SelectedIndex = 0;
        }

        protected void sdsDeviceTracking_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            int sodong = e.AffectedRows;
            lblInfo.Text = "Có " + sodong.ToString() + " dữ liệu trong danh sách";
            if (sodong <= 1)
            {
                this.GridView1.SelectRow(-1);
            }
        }

        protected void txtFilter_TextChanged(object sender, EventArgs e)
        {
            FilterChanged();
        }

        protected void FilterChanged()
        {
            TextBox tb1 = (TextBox)this.GridView1.HeaderRow.FindControl("txtName");            
            TextBox tb2 = (TextBox)this.GridView1.HeaderRow.FindControl("txtFullname");
            TextBox tb3 = (TextBox)this.GridView1.HeaderRow.FindControl("txtImei");
            TextBox tb4 = (TextBox)this.GridView1.HeaderRow.FindControl("txtLocalTime");
            TextBox tb5 = (TextBox)this.GridView1.HeaderRow.FindControl("txtNetworkTime");
            TextBox tb6 = (TextBox)this.GridView1.HeaderRow.FindControl("txtNote");
            TextBox tb7 = (TextBox)this.GridView1.HeaderRow.FindControl("txtUploadTime");

            string tn1 = tb1.Text.Trim() == "" ? "|" : tb1.Text.Trim();
            string tn2 = tb2.Text.Trim() == "" ? "|" : tb2.Text.Trim();
            string tn3 = tb3.Text.Trim() == "" ? "|" : tb3.Text.Trim();
            string tn4 = tb4.Text.Trim() == "" ? "|" : tb4.Text.Trim();
            string tn5 = tb5.Text.Trim() == "" ? "|" : tb5.Text.Trim();
            string tn6 = tb6.Text.Trim() == "" ? "|" : tb6.Text.Trim();
            string tn7 = tb7.Text.Trim() == "" ? "|" : tb7.Text.Trim();            

            string para1 = "txtName";            
            string para2 = "txtFullname";
            string para3 = "txtImei";
            string para4 = "txtLocalTime";
            string para5 = "txtNetworkTime";
            string para6 = "txtNote";
            string para7 = "txtUploadTime";

            sdsDeviceTracking.SelectParameters[para1].DefaultValue = tn1;
            sdsDeviceTracking.SelectParameters[para2].DefaultValue = tn2;
            sdsDeviceTracking.SelectParameters[para3].DefaultValue = tn3;
            sdsDeviceTracking.SelectParameters[para4].DefaultValue = tn4;
            sdsDeviceTracking.SelectParameters[para5].DefaultValue = tn5;
            sdsDeviceTracking.SelectParameters[para6].DefaultValue = tn6;
            sdsDeviceTracking.SelectParameters[para7].DefaultValue = tn7;
            
            this.GridView1.DataBind();

            tb1 = (TextBox)this.GridView1.HeaderRow.FindControl("txtName");            
            tb2 = (TextBox)this.GridView1.HeaderRow.FindControl("txtFullname");
            tb3 = (TextBox)this.GridView1.HeaderRow.FindControl("txtImei");
            tb4 = (TextBox)this.GridView1.HeaderRow.FindControl("txtLocalTime");
            tb5 = (TextBox)this.GridView1.HeaderRow.FindControl("txtNetworkTime");
            tb6 = (TextBox)this.GridView1.HeaderRow.FindControl("txtNote");
            tb7 = (TextBox)this.GridView1.HeaderRow.FindControl("txtUploadTime");

            if (tn1 != "|")
                tb1.Text = tn1;
            if (tn2 != "|")
                tb2.Text = tn2;
            if (tn3 != "|")
                tb3.Text = tn3;
            if (tn4 != "|")
                tb4.Text = tn4;
            if (tn5 != "|")
                tb5.Text = tn5;
            if (tn6 != "|")
                tb6.Text = tn6;
            if (tn7 != "|")
                tb7.Text = tn7;            
        } 

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = this.GridView1.SelectedIndex;

            if (i >= 0)
            {
                string fullname = ((Label)GridView1.Rows[i].Cells[7].FindControl("Label3")).Text;
                string localtime = ((Label)GridView1.Rows[i].Cells[7].FindControl("Label5")).Text;
                string networktime = ((Label)GridView1.Rows[i].Cells[7].FindControl("Label6")).Text;
                double lat = Convert.ToDouble(((Label)GridView1.Rows[i].Cells[7].FindControl("Label10")).Text);
                double lng = Convert.ToDouble(((Label)GridView1.Rows[i].Cells[7].FindControl("Label11")).Text);                
                
                Response.Redirect("~/wfDeviceTrackingDetail.aspx?Fullname=" + fullname + "&LocalTime=" + localtime 
                    + "&NetworkTime=" + networktime + "&Lat=" + lat + "&Lng=" + lng);
            }
        }
    }
}
